﻿
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Routing;

namespace WXWorkSvc.Common.TagHelpers;
//-------------------
// 24.1.2修改完成
//-------------------
/// <summary>
/// 
/// </summary>
[HtmlTargetElement("desk-buttons", TagStructure = TagStructure.WithoutEndTag)]
public class DeskButtonsTagHelper : TagHelper
{
    [HtmlAttributeNotBound]
    [ViewContext]
    public ViewContext ViewContext { get; set; } = default!;
    private readonly  IHtmlGenerator _HtmlGenerator;

    public DeskButtonsTagHelper(IHtmlGenerator generator)
    {
        _HtmlGenerator = generator;
    }

    [HtmlAttributeName("asp-offset")]
    public int Offset { get; set; } = 2;
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-link-text-")]
    public Dictionary<string, string> PageLinkTexts { get; set; } = new();
    /// <summary>
    /// 若为空，则onclick(asp-route-values..)
    /// </summary>
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-page-")]
    public Dictionary<string, string> PageNames { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-fun-")]
    public Dictionary<string, string> FunctionNames { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-link-type-")]
    public Dictionary<string, EButtonType> LinkTypes { get; set; } = new();
    /// <summary>
    /// 常量参数
    /// </summary>
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-const-")]
    public Dictionary<string, object> ConstValues { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-route-")]
    public Dictionary<string, ModelExpression> RouteValues { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-delete-")]
    public Dictionary<string, bool> Deletes { get; set; } = new();

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        output.TagName = "div";
        output.TagMode = TagMode.StartTagAndEndTag;
        output.Attributes.Add("class", "row");

        var tagDiv = new TagBuilder("div");
        tagDiv.AddCssClass($"form-group  col-sm-{12-Offset} offset-sm-{Offset}");


        bool isNotFirst = false;
        foreach (var kv in PageLinkTexts)
        {
            if (Deletes.TryGetValue(kv.Key, out var delete) && delete)
                continue;
            var routeValues = RouteValues.GetRouteValues(ConstValues, kv.Key);
            if (!LinkTypes.TryGetValue(kv.Key, out var linkType))
                linkType = EButtonType.Secondary;
            var cssClass = TagHelperExtend.GetButtonCssClass(linkType, false);
            if (isNotFirst)
                cssClass += " mx-1";

            if (PageNames.Remove(kv.Key, out var pageName))
            {
                var linkTagBuilder = _HtmlGenerator.GeneratePageLink(ViewContext, kv.Value, pageName, null, null, null, null, routeValues, new { @class = cssClass });
                tagDiv.InnerHtml.AppendHtml(linkTagBuilder);
            }
            else if (FunctionNames.Remove(kv.Key, out var functionName))
            {
                var linkTagBuilder = new TagBuilder("a");
                linkTagBuilder.AddCssClass(cssClass);
                linkTagBuilder.Attributes.Add("href", "javascript:;");
                linkTagBuilder.Attributes.Add("onclick", routeValues.ToFunction(functionName));
                linkTagBuilder.InnerHtml.SetHtmlContent(kv.Value);
                tagDiv.InnerHtml.AppendHtml(linkTagBuilder);
            }
            else
            {
                var buttonTagBuilder = new TagBuilder("button");
                buttonTagBuilder.AddCssClass(cssClass);
                buttonTagBuilder.Attributes.Add("type", "submit");
                buttonTagBuilder.InnerHtml.SetContent(kv.Value);
                tagDiv.InnerHtml.AppendHtml(buttonTagBuilder);
            }
            if (!isNotFirst)
                isNotFirst = true;

        }

        output.Content.SetHtmlContent(tagDiv);
    }

}

